package com.yxjx.duoxue.my;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.PageLoading;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.dao.Dao;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.payment.ClientUserKidsDTS;
import com.yxjx.duoxue.payment.KidInfoEditActivity;
import com.yxjx.duoxue.payment.datamodel.DMKidInfoRefObject;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class MyAccountActivity extends BaseActionBarActivity{
	
	private static final int REQ_CODE_EDIT_KID_INFO = 1;
	
	private static final int MSG_LOAD_KID_LABELS_AND_COURSE_TYPES = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_my_account);
		customizeActionBarTitle("我的账号");
		UIHelper.setClickListener(findViewById(R.id.root), R.id.modify_password, mClickListener);
		UIHelper.setClickListener(findViewById(R.id.root), R.id.unloggin, mClickListener);
		UIHelper.setClickListener(findViewById(R.id.root), R.id.addOrEdditKidInfo, mClickListener);
		showLoading(true);

		new Thread(new Runnable() {

			@Override
			public void run() {
				// 尝试加载全部课程类别信息
				PageLoading.getCourseCategory(MyAccountActivity.this);
				// 尝试获取学生特点label全集
				JSONObject jo = new JSONObject();
				try {
					jo.put("date", System.currentTimeMillis());
					UserReg.getUserKids(jo, MyAccountActivity.this, false);
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					mHandler.sendEmptyMessage(MSG_LOAD_KID_LABELS_AND_COURSE_TYPES);
				}
			}
		}).start();
	}
	
	

	private void updateUI() {
		if (Utils.isLogin(getApplicationContext())) {
			String phoneNum = Utils.getValueFromSharedPreference(getApplicationContext(), Yx.KEY_PHONE_NUM);
			UIHelper.setText(findViewById(R.id.root), R.id.phone_num, phoneNum);
			
			ClientUserKidsDTS kidInfo = Dao.getUserKidInfo(this);
			setChildInfoText(kidInfo);
		}
	}
	
	private void setChildInfoText(ClientUserKidsDTS kidInfo) {
		showAsAddNew = (kidInfo == null);
		findViewById(R.id.child_root).setVisibility(showAsAddNew ? View.INVISIBLE : View.VISIBLE);
		findViewById(R.id.child_add).setVisibility(showAsAddNew ? View.VISIBLE : View.INVISIBLE);
		
		if (!showAsAddNew) {
			UIHelper.setText(findViewById(R.id.child_root), R.id.child_name, kidInfo.getKidsName());
			UIHelper.setText(findViewById(R.id.child_root), R.id.child_detail, MyPageFragment.getKidDetail(kidInfo));
		}
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.modify_password:
				startActivity(new Intent(getApplicationContext(), ModifyPasswordActivity.class));
				break;
			case R.id.unloggin:
				unlogin();
				setResult(RESULT_OK);
				finish();
				break;
			case R.id.addOrEdditKidInfo:
				addOrEdditKidInfo();
				break;
			default:
				break;
			}
		}
	};
	private boolean showAsAddNew = true;
	
	private void unlogin() {
		String[] keys = new String[] {
				Yx.KEY_PHONE_NUM,
				Yx.KEY_SESSION_KEY,
				Yx.KEY_SID,
				Yx.KEY_UID,
		};
		for (String key : keys) {
			Utils.writeToSharePreference(getApplicationContext(), key, "");
		}
		Dao.setUserKidInfo(getApplicationContext(), null);
	}
	
	private void addOrEdditKidInfo() {
		Intent intent = new Intent(this, KidInfoEditActivity.class);
		ClientUserKidsDTS extra = Dao.getUserKidInfo(this);
		intent.putExtra(KidInfoEditActivity.KEY_KID_INFO_TOTAL, DMKidInfoRefObject.from(this, extra));
		startActivityForResult(intent, REQ_CODE_EDIT_KID_INFO);
	}
	
	@Override
	protected void onActivityResult(int reqCode, int resultCode, Intent data) {
		if (reqCode == REQ_CODE_EDIT_KID_INFO) {
			switch (resultCode) {
			case RESULT_OK:
				updateUI();
				setResult(RESULT_CANCELED);
				break;
			default:
				break;
			}
		}
		super.onActivityResult(reqCode, resultCode, data);
	}
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOAD_KID_LABELS_AND_COURSE_TYPES:
				updateUI();
				showLoading(false);
				break;
			default:
				break;
			}
		};
	};
	
	private void showLoading(final boolean showing) {
		findViewById(R.id.loading).setVisibility(showing ? View.VISIBLE : View.INVISIBLE);
		findViewById(R.id.root).setVisibility(showing ? View.INVISIBLE : View.VISIBLE);
	}
 }
